package com.nursing.activity;



import com.nursing.common.Configure;
import com.nursing.utils.MyLog;
import com.nursing.utils.Utils;
import com.nursing.utils.connection.BaseExecutor;
import com.nursing.utils.connection.BaseTask;
import com.nursing.utils.connection.OnTaskHandlerListener;

import android.R.color;
import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TabHost;
import android.widget.RadioGroup.OnCheckedChangeListener;

public class NursingActivity extends TabActivity implements OnClickListener,OnTaskHandlerListener,OnCheckedChangeListener {
    /** Called when the activity is first created. */

	public static String TAB_TAG_HOME = "home";
	public static String TAB_TAG_CHANNEL = "channel";
	public static String TAB_TAG_ACCOUNT = "account";
	public static String TAB_TAG_SEARCH = "search";
	public static String TAB_TAB_MORE = "more";

	protected BaseExecutor mConnectionTask;
	public static TabHost mTabHost;
	// Animation
	private Animation left_in, left_out;
	private Animation right_in, right_out;

    private RadioGroup radioGroup;
    private RadioButton radioHome,radioSearch,radioKabao,radioAdvert,radioMore;
    private ImageButton cateBtn;
    private Button loaclBtn;
    private Intent mHomeItent, mSearchIntent, mKabaoIntent, mAdvertIntent,mMoreIntent;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mConnectionTask = new BaseExecutor(this);
		Configure.init(this);
        initView();
        initListener();
		prepareAnim();
    }

    private void initView(){
    	radioGroup = (RadioGroup)findViewById(R.id.main_radiogroup);

		mHomeItent = new Intent(this, HomeActivity.class);
		mSearchIntent = new Intent(this, AskActivity.class);
		mKabaoIntent = new Intent(this, SkinRecommendActivity.class);
		mAdvertIntent = new Intent(this, AccountActivity.class);

		mTabHost = getTabHost();
		mTabHost.addTab(buildTabSpec(TAB_TAG_HOME, R.string.category_home, R.drawable.icon, mHomeItent));
		mTabHost.addTab(buildTabSpec(TAB_TAG_CHANNEL,R.string.category_channel, R.drawable.icon, mSearchIntent));
		mTabHost.addTab(buildTabSpec(TAB_TAG_SEARCH, R.string.category_search,R.drawable.icon, mKabaoIntent));
		mTabHost.addTab(buildTabSpec(TAB_TAG_ACCOUNT,R.string.category_account, R.drawable.icon, mAdvertIntent));
		mTabHost.setCurrentTabByTag(TAB_TAG_HOME);
    }

    private void initListener(){
    	radioGroup.setOnCheckedChangeListener(this);
    }
    private void prepareAnim() {
		left_in = AnimationUtils.loadAnimation(this, R.anim.left_in);
		left_out = AnimationUtils.loadAnimation(this, R.anim.left_out);

		right_in = AnimationUtils.loadAnimation(this, R.anim.right_in);
		right_out = AnimationUtils.loadAnimation(this, R.anim.right_out);
	}


    private TabHost.TabSpec buildTabSpec(String tag, int resLabel, int resIcon,final Intent content) {
		return mTabHost.newTabSpec(tag).setIndicator(getString(resLabel),getResources().getDrawable(resIcon)).setContent(content);
	}

	public static void setCurrentTabByTag(String tab) {
		mTabHost.setCurrentTabByTag(tab);
	}


	@Override
	public void onClick(View v) {

	}


	private void sync(BaseTask task){
		if(!Utils.notNull(mConnectionTask)){
			MyLog.error(getClass(), "---mConnectionTask null------");
			mConnectionTask = new BaseExecutor(this);
		}
		mConnectionTask.connectInQueue(task);
	}
	/**
	 * stop all the thread
	 */
	protected void stopTask(){
		if(Utils.notNull(mConnectionTask)){
			mConnectionTask.disconnect();
			MyLog.debug(getClass(), "------mConnectionTask close----------");
		}
	}

	@Override
	public Object onConnection(int action, Object... params) throws Exception {
		// TODO Auto-generated method stub
		return action;
	}

	@Override
	public void onProcessData(int action, Object data, Object... params) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onException(int action, Exception exception, Object... params) {
		// TODO Auto-generated method stub

	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		MyLog.debug(getClass(), "------------------stopTask-------------------");
		stopTask();
		mConnectionTask = null;
	}

	int mCurTabId = R.id.radio_home;

	@Override
	public void onCheckedChanged(RadioGroup group, int checkedId) {
		// TODO Auto-generated method stub
		MyLog.debug(getClass(), "checkedId:"+checkedId);
		if (mCurTabId ==checkedId) {
			return;
		}
		final boolean o;
		if (mCurTabId < checkedId)
			o = true;
		else
			o = false;
		if (o)
			mTabHost.getCurrentView().startAnimation(left_out);
		else
			mTabHost.getCurrentView().startAnimation(right_out);
		switch (checkedId) {
		case R.id.radio_home:
			mTabHost.setCurrentTabByTag(TAB_TAG_HOME);
			break;
		case R.id.radio_search:
			mTabHost.setCurrentTabByTag(TAB_TAG_CHANNEL);
			break;
		case R.id.radio_kabao:
			mTabHost.setCurrentTabByTag(TAB_TAG_SEARCH);

			break;
		case R.id.radio_advert:
			mTabHost.setCurrentTabByTag(TAB_TAG_ACCOUNT);

			break;

		default:
			break;
		}
		if (o){
			mTabHost.getCurrentView().startAnimation(left_in);
		}else{
			mTabHost.getCurrentView().startAnimation(right_in);
		}
		mCurTabId = checkedId;

		for (int i = 0; i < group.getChildCount(); i++) {
			RadioButton button = (RadioButton) group.getChildAt(i);
			if(button.getId()==mCurTabId){
				button.setBackgroundResource(R.drawable.icon_focus);
//				button.setTextColor(getResources().getColor(R.color.red));
				button.setEnabled(false);
			}else{
//				button.setTextColor(getResources().getColor(R.color.blue));
				button.setBackgroundResource(getResources().getColor(color.transparent));
				button.setEnabled(true);
			}
		}
	}
}